import api from '@/api';

export default {
  namespace: 'bill',

  state: {
    billedSaleBills: {
      list: [],
      pagination: {},
    },
    billedCostBills: {
      list: [],
      pagination: {},
    },
    billedOrders: {
      list: [],
      pagination: {},
    },
    unbilledSaleBills: {
      list: [],
      pagination: {},
    },
    unbilledCostBills: {
      list: [],
      pagination: {},
    },
    cashList: {
      list: [],
      pagination: {},
    },
    adjusts: {
      list: [],
      pagination: {},
    },
    feeType: [],
    adjustStatus: [],
    currentAdjust: {},
    payAdjusts: {
      list: [],
      pagination: {},
    },
    payCurrentAdjust: {},
    currentAduitStatus: '',
    billStatus: [],
    compNames: [],
  },

  effects: {
    *queryInvoiceStatus({ payload }, { call }) {
      return yield call(api.bill.queryInvoiceStatus, payload);
    },
    *queryInvoiceList({ payload }, { call }) {
      return yield call(api.bill.queryInvoiceList, payload);
    },
    *queryInvoiceType({ payload }, { call }) {
      return yield call(api.bill.queryInvoiceType, payload);
    },
    *queryInvoiceById({ payload }, { call }) {
      return yield call(api.bill.queryInvoiceById, payload);
    },
    *invoiceDetailTable({ payload }, { call }) {
      return yield call(api.bill.invoiceDetailTable, payload);
    },
    *fillinNo({ payload }, { call }) {
      return yield call(api.bill.fillinNo, payload);
    },
    *repush({ payload }, { call }) {
      return yield call(api.bill.retry, payload);
    },
    *disable({ payload }, { call }) {
      return yield call(api.bill.disable, payload);
    },
    *queryCheckBillDetailSummary({ payload }, { call }) {
      return yield call(api.bill.queryCheckBillDetailSummary, payload);
    },
    *queryCheckBillDetailTable({ payload }, { call }) {
      return yield call(api.bill.queryCheckBillDetailTable, payload);
    },
    *queryBillCheckList({ payload }, { call }) {
      return yield call(api.bill.queryBillCheckList, payload);
    },
    *queryHuaRuiList({ payload }, { call }) {
      return yield call(api.bill.queryHuaruiList, payload);
    },
    *createBillCheck({ payload }, { call }) {
      return yield call(api.bill.createBillCheck, payload);
    },
    *queryRechargeList({ payload }, { call }) {
      return yield call(api.bill.queryRechargeList, payload);
    },
    *queryPayFeeList({ payload }, { call }) {
      return yield call(api.bill.queryPayFeeList, payload);
    },
    *fetchSaleBilled({ payload }, { call, put }) {
      let res = yield call(api.bill.querySale, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveSaleBills',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchCostBilled({ payload }, { call, put }) {
      let res = yield call(api.bill.queryCost, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCostBills',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchBilledSaleOrders({ payload }, { call, put }) {
      let res = yield call(api.bill.queryBilledSaleOrders, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveBilledOrders',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchBilledCostOrders({ payload }, { call, put }) {
      let res = yield call(api.bill.queryBilledCostOrders, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveBilledOrders',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchSaleUnBilled({ payload }, { call, put }) {
      let res = yield call(api.bill.queryUnBilledSaleOrders, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveSaleUnBills',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchCostUnBilled({ payload }, { call, put }) {
      let res = yield call(api.bill.queryUnBilledCostOrders, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCostUnBills',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *modifyActualFee({ payload }, { call, put }) {
      return yield call(api.bill.modifyActualFee, payload);
    },
    *fetchReceiptCash({ payload }, { call, put }) {
      let res = yield call(api.bill.receipt.cashSettle.query, payload);
      const { data: list, pagination, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'saveCashList',
          payload: {
            list,
            pagination,
          },
        });
      }
      return res;
    },
    *fetchAdjusts({ payload }, { call, put }) {
      let res = yield call(api.bill.receipt.adjust.query, payload);
      const { data: list, pagination, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'saveAdjusts',
          payload: {
            list,
            pagination,
          },
        });
      }
      return res;
    },
    *fetchAdjustsFeeType({ payload }, { call, put }) {
      let res = yield call(api.bill.receipt.adjust.queryFeeType, payload);
      const { data, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'saveAdjustsFeeType',
          payload: data,
        });
      }
    },
    *fetchAdjustsStatus({ payload }, { call, put }) {
      let res = yield call(api.bill.receipt.adjust.queryStatus, payload);
      const { data, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'saveAdjustsStatus',
          payload: data,
        });
      }
    },
    *fetchCustomerByBillNo({ payload }, { call, put }) {
      return yield call(api.bill.receipt.adjust.queryCustomerByBillNo, payload);
    },
    *createAdjust({ payload }, { call, put }) {
      return yield call(api.bill.receipt.adjust.create, payload);
    },
    *updateAdjust({ payload }, { call, put }) {
      return yield call(api.bill.receipt.adjust.update, payload);
    },
    *fetchCurrentAdjust({ payload }, { call, put }) {
      let res = yield call(api.bill.receipt.adjust.queryCurrentAdjust, payload);
      const { data, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'saveCurrentAdjust',
          payload: data,
        });
      }
      return res;
    },
    *deleteAdjust({ payload }, { call, put }) {
      return yield call(api.bill.receipt.adjust.delete, payload);
    },
    *submitAuditAdjust({ payload }, { call, put }) {
      return yield call(api.bill.receipt.adjust.submitAduit, payload);
    },
    *auditAdjust({ payload }, { call, put }) {
      return yield call(api.bill.receipt.audit.audit, payload);
    },
    *verifyBill({ payload }, { call, put }) {
      return yield call(api.bill.receipt.verifyBill, payload);
    },

    *fetchPayAdjusts({ payload }, { call, put }) {
      let res = yield call(api.bill.payment.adjust.query, payload);
      const { data: list, pagination, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'savePayAdjusts',
          payload: {
            list,
            pagination,
          },
        });
      }
      return res;
    },
    *fetchSupplierByBillNo({ payload }, { call, put }) {
      return yield call(api.bill.payment.adjust.querySupplierByBillNo, payload);
    },
    *createPayAdjust({ payload }, { call, put }) {
      return yield call(api.bill.payment.adjust.create, payload);
    },
    *updatePayAdjust({ payload }, { call, put }) {
      return yield call(api.bill.payment.adjust.update, payload);
    },
    *fetchPayCurrentAdjust({ payload }, { call, put }) {
      let res = yield call(api.bill.payment.adjust.queryCurrentAdjust, payload);
      const { data, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'savePayCurrentAdjust',
          payload: data,
        });
      }
      return res;
    },
    *deletePayAdjust({ payload }, { call, put }) {
      return yield call(api.bill.payment.adjust.delete, payload);
    },
    *submitPayAuditAdjust({ payload }, { call, put }) {
      return yield call(api.bill.payment.adjust.submitAduit, payload);
    },
    *auditPayAdjust({ payload }, { call, put }) {
      return yield call(api.bill.payment.audit.audit, payload);
    },
    *queryBillStatus({ payload }, { call, put }) {
      let res = yield call(api.bill.payment.queryBillStatus, payload);
      const { data, errorCode } = res;
      if (errorCode === 0) {
        yield put({
          type: 'saveBillStatus',
          payload: data,
        });
      }
      return res;
    },
    *verifyPayBill({ payload }, { call, put }) {
      return yield call(api.bill.payment.verifyBill, payload);
    },
    *auditPayBill({ payload }, { call, put }) {
      return yield call(api.bill.payment.auditPayBill, payload);
    },
    *getUnVerifiedBill({ payload }, { call, put }) {
      return yield call(api.bill.payment.getUnVerifiedBill, payload);
    },
    *fetchCompNames(_, { call, put }) {
      const res = yield call(api.bill.compNames);
      yield put({
        type: 'saveCompNames',
        payload: res.data,
      });
      return res;
    },
  },

  reducers: {
    saveSaleBills(state, { payload }) {
      return {
        ...state,
        billedSaleBills: payload,
      };
    },
    saveCostBills(state, { payload }) {
      return {
        ...state,
        billedCostBills: payload,
      };
    },
    saveBilledOrders(state, { payload }) {
      return {
        ...state,
        billedOrders: payload,
      };
    },
    saveSaleUnBills(state, { payload }) {
      return {
        ...state,
        unbilledSaleBills: payload,
      };
    },
    saveCostUnBills(state, { payload }) {
      return {
        ...state,
        unbilledCostBills: payload,
      };
    },
    saveCashList(state, { payload }) {
      return {
        ...state,
        cashList: payload,
      };
    },
    saveAdjusts(state, { payload }) {
      return {
        ...state,
        adjusts: payload,
      };
    },
    saveAdjustsFeeType(state, { payload }) {
      return {
        ...state,
        feeType: payload,
      };
    },
    saveAdjustsStatus(state, { payload }) {
      return {
        ...state,
        adjustStatus: payload,
      };
    },
    saveCurrentAdjust(state, { payload }) {
      return {
        ...state,
        currentAdjust: payload,
      };
    },
    clearCurrentAdjust(state) {
      return {
        ...state,
        currentAdjust: {},
      };
    },
    savePayAdjusts(state, { payload }) {
      return {
        ...state,
        payAdjusts: payload,
      };
    },
    savePayCurrentAdjust(state, { payload }) {
      return {
        ...state,
        payCurrentAdjust: payload,
      };
    },
    clearPayCurrentAdjust(state) {
      return {
        ...state,
        payCurrentAdjust: {},
      };
    },
    saveAdjustAduitStatus(state, { payload }) {
      return {
        ...state,
        currentAduitStatus: payload,
      };
    },
    saveBillStatus(state, { payload }) {
      return {
        ...state,
        billStatus: payload,
      };
    },
    saveCompNames(state, { payload }) {
      return {
        ...state,
        compNames: payload,
      };
    },
  },
};
